package com.wuge.gulimall.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.wuge.gulimall.product.aop.RedisQuery;
import com.wuge.gulimall.product.vo.Catelog2Vo;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuge.common.utils.PageUtils;
import com.wuge.common.utils.Query;

import com.wuge.gulimall.product.dao.CategoryDao;
import com.wuge.gulimall.product.entity.CategoryEntity;
import com.wuge.gulimall.product.service.CategoryService;


@Service("/categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    RedissonClient redissonClient;
    @Override
    @Cacheable(value = "category",key = "#root.method")
    public List<CategoryEntity> getCatelog1level() {
        return list(new QueryWrapper<CategoryEntity>().eq("cat_level",1));
    }

@RedisQuery
    public Map<String, List<Catelog2Vo>> getCatelog2level() {
        CategoryServiceImpl categoryService= (CategoryServiceImpl) AopContext.currentProxy();
        Map<String, List<Catelog2Vo>> listMap =categoryService.getCatelog2levelDb();
        return listMap;
    }

//默认每个命名空间对应一个Cache
@Cacheable(value = "category",key = "#root.method")
    public Map<String, List<Catelog2Vo>> getCatelog2levelDb() {
        List<CategoryEntity> catelog1level = getCatelog1level();
        List<CategoryEntity> categoryEntityList = list();
        Map<String, List<Catelog2Vo>> map = catelog1level.stream().collect(Collectors.toMap(k -> {
            return k.getCatId().toString();
        }, lv1 -> {
            List<CategoryEntity> cat_level2 = getCatLevel(categoryEntityList,lv1.getCatId());
            List<Catelog2Vo> catelog2Vos = cat_level2.stream().map((lv2) -> {
                Catelog2Vo catelog2Vo = new Catelog2Vo();
                catelog2Vo.setCatalog1Id(lv2.getParentCid().toString());
                catelog2Vo.setId(lv2.getCatId().toString());
                catelog2Vo.setName(lv2.getName());
                List<CategoryEntity> cat_level3 = getCatLevel(categoryEntityList,lv2.getCatId());
                List<Catelog2Vo.Category3Vo> category3Vos = cat_level3.stream().map(lv3 -> {
                    Catelog2Vo.Category3Vo category3Vo = new Catelog2Vo.Category3Vo();
                    category3Vo.setCatalog2Id(lv2.getCatId().toString());
                    category3Vo.setId(lv3.getCatId().toString());
                    category3Vo.setName(lv3.getName());
                    return category3Vo;
                }).collect(Collectors.toList());
                catelog2Vo.setCatalog3List(category3Vos);
                return catelog2Vo;
            }).collect(Collectors.toList());
            return catelog2Vos;
        }));


        return map;
    }

    private List<CategoryEntity> getCatLevel(List<CategoryEntity> categoryEntityList, Long catId) {
        List<CategoryEntity> list = categoryEntityList.stream().filter((entity -> {
            return entity.getParentCid() == catId;
        })).collect(Collectors.toList());
        return list;
    }



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        List<CategoryEntity> list = list();
//        使用流获取一级分类的子类
        List<CategoryEntity> collect = list.stream().filter((entity) -> {
            return entity.getCatLevel() == 1;
        }).map((entity) -> {
            entity.setChildren(getChildrens(entity, list));
            return entity;
        }).collect(Collectors.toList());
        return collect;
    }
//    递归获取子分类
    public List<CategoryEntity> getChildrens(CategoryEntity root,List<CategoryEntity> list){
        List<CategoryEntity> collect = list.stream().filter((entity) -> {
            return entity.getParentCid().equals(root.getCatId());
        }).map((entity) -> {
            entity.setChildren(getChildrens(entity, list));
            return entity;
        }).sorted((o1, o2) -> {

            return o1.getSort() - o2.getSort();
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<Long> parentId(Long catId) {
        List<Long> list=new ArrayList<>();
        CategoryEntity byId = getById(catId);
        merge(byId,list);
        Collections.reverse(list);
        System.out.println(list);
        return list;
    }
    public void merge(CategoryEntity categoryEntity,List list){
        if (categoryEntity==null){
            return;
        }
        System.out.println(categoryEntity);
        list.add(categoryEntity.getCatId());
        Long parentCid = categoryEntity.getParentCid();
        CategoryEntity byId = getById(parentCid);
        merge(byId,list);


    }
}